package com.ccrfid.rmc6.resource.business.prison.rollcall;

import com.ccrfid.rmc6.entity.business.prison.rollcall.PrisonRollcallResultDetailEntity;
import com.ccrfid.rmc6.entity.business.prison.rollcall.PrisonRollcallResultEntity;
import com.ccrfid.rmc6.entity.business.prison.rollcall.PrisonRollcallTaskTimeEntity;
import com.ccrfid.rmc6.pojo.dto.prison.rollcall.PrisonRollcallPoliceDto;
import com.ccrfid.rmc6.pojo.dto.prison.rollcall.PrisonRollcallResultDto;
import com.ccrfid.rmc6.pojo.param.PageParam;
import com.ccrfid.rmc6.repository.search.Criterion;
import com.ccrfid.rmc6.repository.search.SearchCriteria;
import com.ccrfid.rmc6.service.business.prison.rollcall.PrisonRollcallService;
import com.ccrfid.rmc6.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.sql.Date;
import java.sql.Time;
import java.util.List;

@Path("prison-rollcall")
@Produces(MediaType.APPLICATION_JSON)
public class PrisonRollcallResource {
    private static final Logger logger = LoggerFactory.getLogger(PrisonRollcallResource.class);
    @Autowired
    private PrisonRollcallService rollcallService;

    //-----------------------
    //获取点名任务整点列表
    @GET
    @Path("task-times")
    public List<PrisonRollcallTaskTimeEntity> getRollcallTaskTimeList() {
        return rollcallService.getRollcallTaskTimeList();
    }

    //新增点名任务整点信息
    @POST
    @Path("task-times/{timeValue}")
    public PrisonRollcallTaskTimeEntity createRollcallTaskTime(@PathParam("timeValue") Time timeValue) {
        return rollcallService.createRollcallTaskTime(timeValue);
    }

    //删除点名任务整点信息
    @DELETE
    @Path("task-times/{timeValue}")
    public Boolean deleteRollcallTaskTime(@PathParam("timeValue") Time timeValue) {
        return rollcallService.deleteRollcallTaskTime(timeValue);
    }

    //----------------------
    //查询点名记录
    @GET
    @Path("results")
    public Page<PrisonRollcallResultEntity> getRollcallResultList(@QueryParam("dutyDate") Date dutyDate,
                                                                  @QueryParam("taskTime") Time taskTime,
                                                                  @QueryParam("policeSn") String policeSn,
                                                                  @QueryParam("policeName") String policeName,
                                                                  @QueryParam("placeId") String placeId,
                                                                  @QueryParam("placeName") String placeName,
                                                                  @QueryParam("resultStatus") Integer resultStatus,
                                                                  @BeanParam PageParam pageParam) {
        SearchCriteria criteria = SearchCriteria.Builder.create();
        if (dutyDate != null) {
            java.util.Date startTime = DateUtil.parseDateTimeStr(dutyDate.toString() + " 00:00:00");
            java.util.Date stopTime = DateUtil.parseDateTimeStr(dutyDate.toString() + " 23:59:59");
            criteria.add(new Criterion("rollcallTime", Criterion.Operator.GTE, startTime));
            criteria.add(new Criterion("rollcallTime", Criterion.Operator.LTE, stopTime));
        }
        if (taskTime != null) {
            criteria.add(new Criterion("taskTime", Criterion.Operator.EQ, taskTime));
        }
        if (policeSn != null) {
            criteria.add(new Criterion("policeSn", Criterion.Operator.EQ, policeSn));
        }
        if (policeName != null) {
            criteria.add(new Criterion("policeName", Criterion.Operator.LIKE, '%' + policeName + '%'));
        }
        if (placeId != null) {
            criteria.add(new Criterion("placeId", Criterion.Operator.EQ, placeId));
        }
        if (placeName != null) {
            criteria.add(new Criterion("placeName", Criterion.Operator.LIKE, '%' + placeName + '%'));
        }
        if (resultStatus != null) {
            if (resultStatus.equals(0)) {
                criteria.add(new Criterion("attendanceRate", Criterion.Operator.LTE, 0.9999));
            } else if (resultStatus > 0) {
                criteria.add(new Criterion("attendanceRate", Criterion.Operator.GTE, 0.9999));
            }
        }
        PageRequest pageRequest;
        if (pageParam.getOrders() != null && pageParam.getOrders().size() > 0) {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize(), Sort.by(pageParam.getOrders()));
        } else {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize());
        }
        return rollcallService.getRollcallResultList(criteria, pageRequest);
    }

    //查询某个点名记录的详细点名信息
    @GET
    @Path("results/{resultId}/detail")
    public Page<PrisonRollcallResultDetailEntity> getRollcallDetailResult(@PathParam("resultId") Long rollcallResultId,
                                                                          @QueryParam("personnelSn") String personnelSn,
                                                                          @QueryParam("personnelName") String personnelName,
                                                                          @QueryParam("cardNumber") Long cardNumber,
                                                                          @QueryParam("resultStatus") Integer resultStatus,
                                                                          @BeanParam PageParam pageParam) {
        SearchCriteria criteria = SearchCriteria.Builder.create();
        if (rollcallResultId != null) {
            criteria.add(new Criterion("rollcallResultId", Criterion.Operator.EQ, rollcallResultId));
        }
        if (personnelSn != null) {
            criteria.add(new Criterion("personnelSn", Criterion.Operator.EQ, personnelSn));
        }
        if (personnelName != null) {
            criteria.add(new Criterion("personnelName", Criterion.Operator.LIKE, '%' + personnelName + '%'));
        }
        if (cardNumber != null) {
            criteria.add(new Criterion("cardNumber", Criterion.Operator.EQ, cardNumber));
        }
        if (resultStatus != null) {
            if (resultStatus.equals(0)) {
                criteria.add(new Criterion("resultStatus", Criterion.Operator.EQ, 0));
            } else if (resultStatus > 0) {
                criteria.add(new Criterion("resultStatus", Criterion.Operator.GT, 0));
            }
        }
        PageRequest pageRequest;
        if (pageParam.getOrders() != null && pageParam.getOrders().size() > 0) {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize(), Sort.by(pageParam.getOrders()));
        } else {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize());
        }
        return rollcallService.getRollcallDetailResult(criteria, pageRequest);
    }

    //提交点名结果
    @POST
    @Path("results")
    @Consumes(MediaType.APPLICATION_JSON)
    public Boolean uploadRollcallResult(PrisonRollcallResultDto resultDto) {
        return rollcallService.uploadRollcallResult(resultDto);
    }

    //获取所有点名警员信息
    @GET
    @Path("policemans")
    public List<PrisonRollcallPoliceDto> getRollcallPolicemanList() {
        return rollcallService.getRollcallPolicemanList();
    }

}
